﻿using Hxj.Data;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecruitStudents.Client.BLL.Impl
{
    public class BLLOnSiteSign
    {
        DAL.DALOnSiteSign dal = new DAL.DALOnSiteSign();
        public DataTable GetTB_Pad_Examing(string strSignedID, string strExamRoomNO, string strExamYear)
        {
            string sqlKS = @"
                            select SignedID,Name AS StudentName,AllowExamID,B.ExamRoomName 
                                from TB_Pad_Examing		A 
                                left join TB_Exam_ExamRoom	B ON A.ExamRoomNO = B.ExamRoomNO 
                                where A.ExamRoomNO = '{0}' and A.ExamYear='{1}' and A.SignedID='{2}'
                                order by LotNO 
                            ";
            sqlKS = string.Format(sqlKS, strExamRoomNO, strExamYear, strSignedID);
            return dal.GetTable(sqlKS);
        }


        public DataTable GetTB_Pad_SignedTableTopOne_New(string strEntretenimientohID, string strExamYear)
        {
            return dal.GetTB_Pad_SignedTableTopOne_New(strEntretenimientohID, strExamYear);
        }
        public TB_Exam_Signed GetExamSignedByID(string strSignedID, string strExamYear)
        {
            return dal.GetModel<TB_Exam_Signed>(TB_Exam_Signed._.SignedID == strSignedID && TB_Exam_Signed._.ExamYear == strExamYear);
        }
        public DataTable CheckTB_Pad_Signed_New(string SpecialityKindsNO, string ExamLevel, string ExamPlaceNO, string idCard, string strExamYear)
        {
            return dal.CheckTB_Pad_Signed_New(SpecialityKindsNO, ExamLevel, ExamPlaceNO, idCard, strExamYear);
        }
        public DataTable GetTB_Pad_SignedTable_New(string strEntretenimientohID, string strExamYear)
        {
            return dal.GetTB_Pad_SignedTable_New(strEntretenimientohID, strExamYear);
        }
        public DataTable CheckTB_Pad_Signed(string SpecialityKindsNO, string ExamLevel, string ExamPlaceNO, string idCard)
        {
            return dal.CheckTB_Pad_Signed(SpecialityKindsNO, ExamLevel, ExamPlaceNO, idCard);
        }

        public DataTable GetTB_Pad_SignedTableByIdCard(string strEntretenimientohID, string strIdentityCard, string strExamYear)
        {
            return dal.GetTB_Pad_SignedTableByIdCard(strEntretenimientohID, strIdentityCard, strExamYear);
        }
        public DataTable GetTB_Pad_SignedTableTopOne(string strEntretenimientohID, string isOrder)
        {
            return dal.GetTB_Pad_SignedTableTopOne(strEntretenimientohID, isOrder);
        }

        public DataTable GetTB_Pad_SignedTable(string strEntretenimientohID, string isOrder)
        {
            return dal.GetTB_Pad_SignedTable(strEntretenimientohID, isOrder);
        }

        public DataTable GetCombinationData(string strExamRoomNo, string strExamYear)
        {
            return dal.GetCombinationData(strExamRoomNo, strExamYear);
        }

        public int GetQueueCount(string ExamRoomID)
        {
            return dal.GetQueueCount(ExamRoomID);
        }

        public JsonMessage UpdateExamSigned(string strSignedID, string strExamYear)
        {
            TB_Exam_Signed model = dal.GetModel<TB_Exam_Signed>(TB_Exam_Signed._.SignedID == strSignedID && TB_Exam_Signed._.ExamYear == strExamYear);
            JsonMessage json = new JsonMessage();
            if (model != null)
            {
                model.Attach();
                DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
                try
                {
                    model.SignedTime = DateTime.Now;
                    dal.Save<TB_Exam_Signed>(model, trans);
                    dal.ExecuteNonQuery("Update TB_Exam_SignUpStudentsSpeciality set ExamState='1' where SignUpStudentsSpecialityID='" + model.SignUpStudentsSpecialityID + "'", trans);
                    trans.Commit();
                    json.State = "true";
                    json.Msg = "操作成功";
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    json.State = "false";
                    json.Msg = "操作失败:" + ex.Message;
                }
                finally
                {
                    trans.Close();
                }
            }
            else
            {
                json.State = "false";
                json.Msg = "操作失败:获取抽签数据(TB_Exam_Signed)失败";
            }
            return json;
        }

        public JsonMessage SaveExamSigned(string strJson)
        {
            JsonMessage json = new JsonMessage();
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                TB_Exam_Signed model = strJson.ToList<TB_Exam_Signed>();
                model.SignedTime = DateTime.Now;
                dal.Save<TB_Exam_Signed>(model, trans);
                //dal.ExecuteNonQuery("Update TB_Sys_ExamPlaceSpecialityKindsGroupMapping set NORecord=ISNULL(NORecord,'')+'," + model.LotNO + "' where GroupNo='" + model.GroupNO + "'", trans);
                dal.ExecuteNonQuery("Update TB_Exam_SignUpStudentsSpeciality set ExamState='1' where SignUpStudentsSpecialityID='" + model.SignUpStudentsSpecialityID + "'", trans);
                trans.Commit();
                json.State = "true";
                json.Msg = "操作成功";
            }
            catch (Exception ex)
            {
                trans.Rollback();
                json.State = "false";
                json.Msg = "操作失败:" + ex.Message;
            }
            finally
            {
                trans.Close();
            }
            return json;
        }

        public JsonMessage DeleteExamSigned(string strSignedID)
        {
            JsonMessage json = null;
            json = new JsonMessage();
            try
            {
                TB_Exam_Signed model = dal.GetModel<TB_Exam_Signed>(TB_Exam_Signed._.SignedID == strSignedID);
                if (model != null)
                {
                    dal.ExecuteNonQuery("Update TB_Exam_Signed set SignedTime=NULL where SignedID='" + strSignedID + "'");
                    //string strGroupNo = model.GroupNO;
                    //string strLotNo = model.LotNO;
                    //string strSignUpStudentsSpecialityID = model.SignUpStudentsSpecialityID;
                    //TB_Sys_ExamPlaceSpecialityKindsGroupMapping modelGroup = dal.GetModel<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo == strGroupNo);
                    //if (modelGroup != null)
                    //{
                    //    string strNoRecad = modelGroup.NORecord;
                    //    string strNoRecadNew = string.Empty;
                    //    string[] strNoRecads = strNoRecad.Split(',');
                    //    foreach (string strNo in strNoRecads)
                    //    {
                    //        if (strNo != strLotNo)
                    //        {
                    //            if (strNoRecadNew == string.Empty)
                    //            {
                    //                strNoRecadNew = strNo;
                    //            }
                    //            else
                    //            {
                    //                strNoRecadNew = strNoRecadNew + ',' + strNo;
                    //            }
                    //        }
                    //    }
                    //    modelGroup.NORecord = strNoRecadNew;
                    //    dal.Save<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(modelGroup);
                    //}
                    //dal.Delete<TB_Exam_Signed>(TB_Exam_Signed._.SignedID == strSignedID);
                }
                json.State = "true";
                json.Msg = "操作成功";
            }
            catch (Exception ex)
            {
                json.State = "false";
                json.Msg = "操作失败:" + ex.Message;
            }
            return json;
        }

        public JsonMessage SavePadQueue(string strJson)
        {
            JsonMessage json = null;
            json = new JsonMessage();
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                TB_Pad_Queue model = strJson.ToList<TB_Pad_Queue>();
                dal.ExecuteNonQuery("Update TB_Exam_Signed set [State]=0 where SignedID='" + model.SignedID + "'", trans);
                dal.Save<TB_Pad_Queue>(model, trans);
                trans.Commit();
                json.State = "true";
                json.Msg = "操作成功";
            }
            catch (Exception ex)
            {
                trans.Rollback();
                json.State = "false";
                json.Msg = "操作失败:" + ex.Message;
            }
            finally
            {
                trans.Close();
            }
            return json;
        }

        public JsonMessage SavePadQueueBySignedID(string strSignedID, string ExamRoomID)
        {
            JsonMessage json = null;
            json = new JsonMessage();
            using (DataTable dt = dal.GetTB_Pad_SignedTable(strSignedID))
            {
                if (dt.Rows.Count > 0)
                {
                    TB_Pad_Queue model = new TB_Pad_Queue();
                    model.SignedID = dt.Rows[0]["SignedID"].ToSafeString();
                    model.AllowExamID = dt.Rows[0]["AllowExamID"].ToSafeString();
                    model.CreateTiime = DateTime.Now;
                    model.ExamProvince = dt.Rows[0]["ExamProvince"].ToSafeString();
                    model.ExamRoomNO = ExamRoomID;
                    model.ExamYear = dt.Rows[0]["ExamYear"].ToSafeString();
                    model.GroupNO = dt.Rows[0]["GroupNO"].ToSafeString();
                    model.IdentityCard = dt.Rows[0]["IdentityCard"].ToSafeString();
                    model.LotNO = dt.Rows[0]["LotNO"].ToSafeString();
                    model.Name = dt.Rows[0]["Name"].ToSafeString();
                    model.NMT_SignUpID = dt.Rows[0]["NMT_SignUpID"].ToSafeString();
                    model.Remark = "";
                    try
                    {
                        SavePadQueue(model.ToJson());
                        json.State = "true";
                        json.Msg = "操作成功";
                    }
                    catch (Exception ex)
                    {
                        json.State = "false";
                        json.Msg = "操作失败:" + ex.Message;
                    }
                }
                else
                {
                    json.State = "false";
                }
            }
            return json;
        }

        public JsonMessage DeletePadQueue(string strSignedID)
        {
            JsonMessage json = null;
            json = new JsonMessage();
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                dal.Delete<TB_Pad_Queue>(TB_Pad_Queue._.SignedID == strSignedID, trans);
                dal.ExecuteNonQuery("update TB_Exam_Signed set State = 1 where SignedID ='" + strSignedID + "'", trans);
                trans.Commit();
                json.State = "true";
                json.Msg = "操作成功";
            }
            catch (Exception ex)
            {
                trans.Rollback();
                json.State = "false";
                json.Msg = "操作失败:" + ex.Message;
            }
            finally
            {
                trans.Close();
            }
            return json;
        }
    }
}
